package vincent.com.layouttest;

import android.content.Context;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.dx.stock.ProxyBuilder;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import vincent.com.layouttest.util.LogUtil;
import vincent.com.layouttest.util.ReflectHelper;

/**
 * Created by zvin-chen on 16/12/4.
 */

public class DynamicProxyActivity extends ColorStatusActivity {
    private StorageManager mStorageManager;
    private Object mStorageListener;

    private Class storageEventListenerClass;

    @Override
    public View onCreateView(ViewGroup rootView) {
        return LayoutInflater.from(this).inflate(R.layout.dynamic_proxy_activity, rootView, true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //      public void unregisterListener(StorageEventListener listener)
        ReflectHelper.callDeclaredMethod(mStorageManager, "unregisterListener", new Class<?>[]{storageEventListenerClass}, new Object[]{mStorageListener});
    }

    public void javaDynamicProxy(View view){
        //java动态代理
        Proxy.newProxyInstance(this.getClassLoader(), new Class[]{DynamicInterface.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if("action".equals(method.getName())){
                            //执行代理操作
                            LogUtil.i("czw_proxy", "代理方法被执行");
                            return null;
                        }

                        return method.invoke(proxy, args);
                    }
                });
    }

    public interface DynamicInterface{
        public void action();
    }

    public void dexmakerDynamicProxy(View view){
        mStorageManager = (StorageManager) this.getSystemService(Context.STORAGE_SERVICE);

        try{
            storageEventListenerClass = Class.forName("android.os.storage.StorageEventListener");
            mStorageListener = ProxyBuilder.forClass(storageEventListenerClass)
                    .dexCache(getApplicationContext().getDir("dx", Context.MODE_PRIVATE))
                    .handler(mInvocateHandler)
                    .build();

            //        public void registerListener(StorageEventListener listener)
            ReflectHelper.callDeclaredMethod(mStorageManager, "registerListener", new Class<?>[]{storageEventListenerClass}, new Object[]{mStorageListener});
        }catch (Exception e){
            LogUtil.e("czw_proxy", e.getMessage());
        }
    }

    private InvocationHandler mInvocateHandler = new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            if("onStorageStateChanged".equals(method.getName())){
                String path = (String) args[0];
                String oldState = (String) args[1];
                String newState = (String) args[2];
                LogUtil.i("czw_proxy", "onStorageStateChanged called! path--->" + path + " | oldState--->"
                        + oldState + " | newState--->" + newState);
                return null;
            }
            return ProxyBuilder.callSuper(proxy, method, args);
        }
    };
}
